package com.wephone.Student.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.AskForFanliResponse;
import com.wephone.Student.bean.data.AskForFanliReq;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.ShareReferenceUtil;

public class AskForFanli2Activity extends BaseActivity {
	private EditText fanliEdit1;
	private EditText fanliEdit2;
	private EditText fanliEdit3;
	private String country;
	private String city;
	private String school1;
	private String school2;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(AskForFanli2Activity.this,AskForFanliResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					AskForFanliResponse res = (AskForFanliResponse)response;
					ShareReferenceUtil.saveApplyIds(getApplicationContext(), res.getIds());
					
					Toast.makeText(getApplicationContext(), "返利申请已提交", Toast.LENGTH_SHORT).show();
					setResult(11);
					finish();
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ask_for_fanli_2);
		
		init();
	}
	
	private void init(){
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			country = bundle.getString("fanli_edit_1");
			city = bundle.getString("fanli_edit_2");
			school1 = bundle.getString("fanli_edit_3");
			school2 = bundle.getString("fanli_edit_4");
		}
		
		fanliEdit1 = (EditText)findViewById(R.id.fanli_edit_1);
		fanliEdit2 = (EditText)findViewById(R.id.fanli_edit_2);
		fanliEdit3 = (EditText)findViewById(R.id.fanli_edit_3);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(TextUtils.isEmpty(fanliEdit1.getText().toString())){
					Toast.makeText(getApplicationContext(), "姓名必须填写", Toast.LENGTH_SHORT).show();
				}else if(TextUtils.isEmpty(fanliEdit2.getText().toString())){
					Toast.makeText(getApplicationContext(), "电话必须填写", Toast.LENGTH_SHORT).show();
				}else if(TextUtils.isEmpty(fanliEdit3.getText().toString())){
					Toast.makeText(getApplicationContext(), "邮箱必须填写", Toast.LENGTH_SHORT).show();
				}else{
					RequestParams params = new RequestParams();
					params.put("aUid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					AskForFanliReq fanliReq = new AskForFanliReq();
					fanliReq.setCountry(country);
					fanliReq.setCity(city);
					fanliReq.setSchool1(school1);
					fanliReq.setSchool2(school2);
					fanliReq.setName(fanliEdit1.getText().toString());
					fanliReq.setTel(fanliEdit2.getText().toString());
					fanliReq.setEmail(fanliEdit3.getText().toString());
					Gson gson = new Gson();
					params.put("aDetail", gson.toJson(fanliReq));
					
					HttpClient.getInstance().post(Constants.API_ADD_APPLY_INFO_URL, params, getHandler());
				}
			}
		});
	}
	

}
